#!/bin/sh
#8.06.11 rodin.s: i18n

export TEXTDOMAIN=xsaneshell #usr_sbin2
export TEXTDOMAINDIR=/usr/share/locale
export OUTPUT_CHARSET=UTF-8

WAIT_MSG="$(gettext "Please wait...")"

usb_scanner() {
	# splash
	gtkdialog-splash -close never -text "$WAIT_MSG" &
	upid=$!
	# find scanner or list of scanners
	LIST=`scanimage -L`
	printf %s "$LIST"|sed -e 's/^device //g' -e 's/is a //g'| tr -d '\`'|tr "\'" "\|"|grep -vE '\/dev\/video[0-9]' > /tmp/scanner
	kill -9 $upid
	
	[ `wc -l /tmp/scanner| cut -d ' ' -f1` -gt 1 ] && SCANNER_BLURB="$(gettext "<b>The following scanners are on your system:</b>")" \
		SELECT_BLURB="$(gettext "Select the appropriate device from the list.")" \
		|| SCANNER_BLURB="$(gettext "<b>The following scanner is on your system:</b>")" \
		SELECT_BLURB="$(gettext "Please select this device.")"

	export Xsane1='<window title="'$(gettext 'Frontend for Xsane')'" icon-name="gtk-preferences">
		<vbox space-expand="true" space-fill="true">
		  '"`/usr/lib/gtkdialog/xml_info scale scanner.svg 60 "$(gettext "<b>When Xsane starts, your scanner (if it is connected and turned on) should be autodetected.</b>")"`"'
		  <frame>
		    <vbox border-width="10">
		      <text xalign="0" use-markup="true"><label>"'$SCANNER_BLURB'"</label></text>
		      <text xalign="0" use-markup="true"><label>"'$SELECT_BLURB'"</label></text>
		    </vbox>
		    <tree column-header-active="false|false|false" column-resizeable="false|false|false" tooltip-text="'$(gettext "Click the appropriate entry.")'">
		      <label>Scanner|Description</label>
		      <variable>SELECTION</variable>
		      <width>500</width>
		      <height>250</height>
		      <input>cat /tmp/scanner</input>
		      <action signal="button-release-event">exit:chosen</action>
		    </tree>
		  </frame>
		</vbox>
		</window>'
	
	. /usr/lib/gtkdialog/xml_info gtk #build bg_pixmap for gtk-theme
	eval `gtkdialog --program=Xsane1`
	case $EXIT in
	chosen)SELECTED=$(grep "$SELECTION" /tmp/scanner|cut -d '|' -f1)
		# write config
		echo "Scanner=$SELECTED" > $HOME/.xsaneshellrc
		rm /tmp/scanner ;;
	*)exit ;;
	esac
}

old_scanner() {
	COMMOUT="`cat /etc/sane.d/dll.conf | grep '^#[a-zA-Z0-9]' | tr '#' ' ' | tr '\n' ' '`"
	
	export Xsane2='
	<window title="'$(gettext 'Frontend for Xsane')'" icon-name="gtk-preferences">
	<vbox space-expand="true" space-fill="true">
	  '"`/usr/lib/gtkdialog/xml_info scale scanner.svg 60 "$(gettext "<b>When Xsane starts, your scanner (if it is connected and turned on) should be autodetected.</b>")" "$(gettext "However, some entries in the drivers list (/etc/sane.d/dll.conf) are commented-out. These are:")" "<i>$COMMOUT</i>" "$(gettext "If your scanner is one of these, then Xsane will not auto-detect it. - in that case, click 'Quit' button and open 'dll.conf' in a text editor and uncomment the appropriate entry.")"`"'
	  <frame>
	    <vbox space-expand="true" space-fill="true">
	      <text height-request="5" space-expand="false" space-fill="false"><label>""</label></text>
	      <expander>  
	        <vbox border-width="10">
	          <text xalign="0" use-markup="true"><label>"'$(gettext "Xsane may be a bit 'insane' when detecting a SCSI scanner. There are various things that you may have to do:
	1. Run 'sane-find-scanner' in a terminal window.
	2. Make sure 'sg' module is loaded.
	3. Specify the device on the commandline,
	   ex: <b># xsane microtek2:/dev/sg3</b>
	4. or maybe a symbolic link,
	   ex: <b># ln -s /dev/sg3 /dev/scanner</b>")'"</label></text>
	          <hseparator></hseparator>
	        </vbox>
	      <label>'$(gettext 'About SCSI drives')'</label>
	    </expander>
	    <text xalign="1" use-markup="true" wrap="false"><label>"'$(gettext "To continue and run Xsane, please choose the scanner type.")'"</label></text>
	  </vbox>
	  <hbox space-expand="false" space-fill="false">
	    <button image-position="2">
	      '"`/usr/lib/gtkdialog/xml_button-icon plug_usb.svg huge`"'
	      <label>USB</label>
	      <action>EXIT:10</action>
	    </button>
	    <button image-position="2">
	      '"`/usr/lib/gtkdialog/xml_button-icon plug_parallel.svg huge`"'
	      <label>"'$(gettext 'Parallel')'"</label>
	      <action>EXIT:11</action>
	    </button>
	    <button image-position="2">
	      '"`/usr/lib/gtkdialog/xml_button-icon plug_scsi.svg huge`"'
	      <label>SCSI</label>
	      <action>EXIT:12</action>
	    </button>
	    </hbox>
	  </frame>
	  <hbox space-expand="false" space-fill="false">
	    <button>
	      '"`/usr/lib/gtkdialog/xml_button-icon quit`"'
	      <label>"'$(gettext 'Quit')'"</label>
	    </button>
	  </hbox>
	</vbox>
	</window>'
	
	. /usr/lib/gtkdialog/xml_info gtk #build bg_pixmap for gtk-theme
	RETVAL="`gtkdialog -p Xsane2`"
	eval "$RETVAL"
	
	case $RETVAL in
	 *10*)
	  #modprobe scanner
	  #2.6 kernel does not have module 'scanner'. instead relies on libusb.
	  echo
	  ;;
	 *11*)
	  modprobe parport_pc
	  ;;
	 *12*)
	  modprobe sg
	  ;;
	 *)
	  exit
	  ;;
	esac
	
	sleep 1
}

if [ ! -f "$HOME/.xsaneshellrc" ];then
	# splash
	gtkdialog-splash -bg green -close never -text "$WAIT_MSG" &
	spid=$!
	# usefull for USB scanners; doesn't detect scsi and parallel port scanners
	sane-find-scanner | grep -q '^found'
	ret=$?
	kill -9 $spid
	
	# based on result we show the right GUI
	case $ret in
		0) usb_scanner ;;
		1) old_scanner ;;
	esac
fi
# run xsane
if [ -f "$HOME/.xsaneshellrc" ];then
	. $HOME/.xsaneshellrc # read config
	exec xsane "$Scanner"
else
	exec xsane
fi
